package com.nengchuan.eic.nceconomy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eic.psm.sdk.entity.CoreSection;
import com.eic.psm.sdk.vo.CoreSectionDepartInfoVo;
import com.eic.review.sdk.vo.TechnoProjectInfoVo;
import com.eic.review.sdk.vo.TechnoProjectVo;
import com.nengchuan.eic.nceconomy.constant.CommonConstant;
import com.nengchuan.eic.nceconomy.constant.OrgCategorySortEnum;
import com.nengchuan.eic.nceconomy.entity.dto.ExcelDetailDTO;
import com.nengchuan.eic.nceconomy.entity.dto.OrgInfoDTO;
import com.nengchuan.eic.nceconomy.entity.dto.RoleDistributionDTO;
import com.nengchuan.eic.nceconomy.entity.vo.*;
import com.nengchuan.eic.nceconomy.fegin.OrgFeignService;
import com.nengchuan.eic.nceconomy.fegin.PpaFeignService;
import com.nengchuan.eic.nceconomy.fegin.ProcessFeignTechnicalCassicsService;
import com.nengchuan.eic.nceconomy.service.RoleMangeService;
import com.nengchuan.eic.nceconomy.service.UserRoleService;
import com.nengchuan.model.model.WrappedResult;
import com.nengchuan.util.NullCheckUtils;
import com.nsc.boot.common.msg.Result;
import com.nsc.boot.common.sdk.msg.ApiResult;
import com.nsc.boot.common.sdk.vo.IPage;
import com.nsc.boot.common.utils.page.PageQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 功能描述:
 *
 * @Author: liurui
 * @Date: 2020/5/15 0015 下午 2:57
 */
@RestController
@RequestMapping("/role")
@Api(value = "RoleMangeController", tags = "角色管理")
public class RoleMangeController {

    @Autowired
    private RoleMangeService roleMangeService;

    @Autowired
    private OrgFeignService orgFeignService;

    @Autowired
    private PpaFeignService ppaFeignService;



    @Autowired
    private UserRoleService userRoleService;

    @ApiOperation("角色分配")
    @PostMapping(value = "/distribution", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<String> distribution(@RequestBody RoleDistributionDTO roleDistributionDTO) {
        if(NullCheckUtils.isNull(roleDistributionDTO.getOrgId())||NullCheckUtils.isNull(roleDistributionDTO.getRoleId())||NullCheckUtils.isNull(roleDistributionDTO.getOrgCategoryCode())){
            return WrappedResult.fail("参数异常");
        }
        return WrappedResult.success(roleMangeService.distribution(roleDistributionDTO));
    }

    @ApiOperation("单项工程项目分配人员")
    @PostMapping(value = "/distribution_project", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<String> distributionProject(@RequestBody RoleDistributionDTO roleDistributionDTO) {
        if(NullCheckUtils.isNull(roleDistributionDTO.getType())||NullCheckUtils.isNull(roleDistributionDTO.getUserId())||NullCheckUtils.isNull(roleDistributionDTO.getUserName())||NullCheckUtils.isNull(roleDistributionDTO.getProId())){
            return WrappedResult.fail("参数异常");
        }
        return WrappedResult.success(roleMangeService.distributionProject(roleDistributionDTO));
    }

    @ApiOperation("查询单位下角色信息")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<List<OrgRole>> getRoleListInfo(@RequestParam("orgId") Long orgId) {
        if(NullCheckUtils.isNull(orgId)){
            return WrappedResult.fail("参数异常");
        }
        return WrappedResult.success(roleMangeService.getRoleListInfo(orgId));
    }
    @ApiOperation("查询专责列表")
    @PostMapping(value = "/duty_list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<Map<String ,List<DutyInfoVO>>> getDutyList(@RequestParam("orgId")Long orgId) {
        if(NullCheckUtils.isNull(orgId)){
            return WrappedResult.fail("参数异常");
        }
        return WrappedResult.success(roleMangeService.getDutyList(orgId));
    }


    @ApiOperation("角色删除人员")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<String> delete(@RequestBody RoleDistributionDTO roleDistributionDTO) {
        if(NullCheckUtils.isNull(roleDistributionDTO.getOrgId())||NullCheckUtils.isNull(roleDistributionDTO.getRoleId())){
            return WrappedResult.fail("参数异常");
        }
        return WrappedResult.success(roleMangeService.delete(roleDistributionDTO));
    }

    @ApiOperation("角色删除人员--项目专责")
    @PostMapping(value = "/delete_on_project", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponse(code = 200, message = "成功为true,失败为false", response = WrappedResult.class)
    public WrappedResult<String> deleteOnProject(@RequestBody RoleDistributionDTO roleDistributionDTO) {
        if(NullCheckUtils.isNull(roleDistributionDTO.getProId())||NullCheckUtils.isNull(roleDistributionDTO.getUserId())||NullCheckUtils.isNull(roleDistributionDTO.getType())){
            return WrappedResult.fail("参数异常");
        }
        return WrappedResult.success(roleMangeService.deleteOnProject(roleDistributionDTO));
    }


    @ApiOperation("获取单位分类")
    @GetMapping("/get_org_category")
    public WrappedResult getOrgCategory(){
        List<OrgCategoryVO> categoryVOS=orgFeignService.getOrgCategory();
        //为每个元素设置排序值，并进行排序
        categoryVOS.forEach(e-> e.setSort(OrgCategorySortEnum.getSortByName(e.getName())));
        categoryVOS=categoryVOS.stream().sorted(Comparator.comparing(OrgCategoryVO::getSort).reversed()).collect(Collectors.toList());
        return WrappedResult.success(categoryVOS);
    }

    @ApiOperation("根据分类获取单位信息")
    @GetMapping(value = "/get_org_info")
    public WrappedResult getOrgInfoByCategory(@ApiParam("分类ID") @RequestParam(required = false) Long categoryId,
                                              @ApiParam("页码") @RequestParam Integer page,
                                              @ApiParam("大小") @RequestParam Integer size){
        Page<OrgInfoDTO> orgInfoVOPage =orgFeignService.getOrgInfo(categoryId,page,size);
        userRoleService.fillManager(orgInfoVOPage.getRecords());
        return WrappedResult.success(orgInfoVOPage);
    }

    @ApiOperation("获取单位下的用户")
    @GetMapping("/get_org_users")
    public WrappedResult getOrgUsers(@ApiParam("单位ID") @RequestParam Long orgId,
                                     @ApiParam("名字") @RequestParam(required = false) String name,
                                     @ApiParam("页码") @RequestParam Integer page,
                                     @ApiParam("大小") @RequestParam Integer size){
        return WrappedResult.success(orgFeignService.getOrgUsers(orgId,name,page,size));
    }

    @ApiOperation("根据单项工程id获取标段信息")
    @GetMapping("/get_coreS_section_by_indivdual_project_id")
    public WrappedResult<List<CoreSectionVO>> getCoreSectionByIndivdualProjectId(@ApiParam("单项工程id") @RequestParam Long projectId){
        if(NullCheckUtils.isNull(projectId)){
            return WrappedResult.fail("参数不能为空");
        }
        Result<List<CoreSection>> coreSection = ppaFeignService.getCoreSectionByIndivdualProjectId(projectId);
        Result<List<CoreSectionDepartInfoVo>> departInfo = ppaFeignService.getCoreSectionDepartInfoByIndivdualProjectId(projectId);
        if(departInfo!=null&& CommonConstant.responseCode.SUCCESS==departInfo.getCode()
        &&coreSection!=null&&CommonConstant.responseCode.SUCCESS==coreSection.getCode()){
           return WrappedResult.success(roleMangeService.getCoreSectionInfo(coreSection.getData(),departInfo.getData()));
        }
        return WrappedResult.success();
    }


    @ApiOperation("获取项目基础信息(省公司，建管单位，设计单位）")
    @GetMapping({"/getTechnoProjectInfo/{projectId}"})
    WrappedResult<List<ProjectInfo>> getTechnoProjectInfo(@ApiParam("单项工程id") @PathVariable(name = "projectId",required = true) Long projectId){
        return WrappedResult.success(roleMangeService.getTechnoProjectInfo(projectId));
    }

    @ApiOperation("技经单位id以及单位类型，获取关联的输变电以及单项工程信息")
    @GetMapping({"/getTechnoProjectList"})
    WrappedResult<IPage<OrgProject>> getTechnoProjectList(@ApiParam("当前页")@RequestParam("current") Integer current,
                                                         @ApiParam("每页数量")@RequestParam("size") Integer size,
                                                         @ApiParam("单位类型：29：省公司 5：建管单位 32：设计单位 33：评审单位") @RequestParam("unitType") Integer unitType,
                                                         @ApiParam("项目名称") @RequestParam(required = false) String projectName,
                                                         @ApiParam("单位id") @RequestParam("unitId") Long unitId,
                                                         @ApiParam("ERP编码") @RequestParam(required = false) String erpCode,
                                                         @ApiParam("项目阶段") @RequestParam(required = false) String projectStage
                                                         ){
        PageQuery pageQuery = new PageQuery();
        pageQuery.setCurrent(current);
        pageQuery.setSize(size);
        return WrappedResult.success(roleMangeService.getTechnoProjectList(pageQuery, unitType, projectName, unitId, erpCode,projectStage));
    }

}
